package com.longdaji.decoration.ui.signin;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.SignInDataBean;
import com.longdaji.decoration.ui.lottery.ScoreLotteryActivity;
import com.longdaji.decoration.widget.SignInDialog;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class SignInActivity extends BaseActivity implements SignInContract.View {

    private static final String TAG = SignInActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.tv_sign_in)
    TextView mTvSignIn;
    @BindView(R.id.iv_first_day)
    ImageView mIvFirst;
    @BindView(R.id.iv_second_day)
    ImageView mIvSecond;
    @BindView(R.id.iv_third_day)
    ImageView mIvThird;
    @BindView(R.id.iv_fourth_day)
    ImageView mIvFour;
    @BindView(R.id.iv_fifth_day)
    ImageView mIvFive;
    @BindView(R.id.iv_sixth_day)
    ImageView mIvSix;
    @BindView(R.id.iv_seventh_day)
    ImageView mIvSenven;
    @BindView(R.id.tv_continue_days)
    TextView mTvDays;
    @BindView(R.id.tv_score)
    TextView mTvScore;
    @Inject
    SignInPresenter mPresenter;
    private SignInDialog dialog;
    private SignInDataBean mSignInDataBean;


    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, SignInActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "ShippingAddrActivity onCreate");
        setContentView(R.layout.activity_signin);
        setUnBinder(ButterKnife.bind(this));
        initView();
    }

    private void initView() {
        mPresenter.setView(this);
        mTvTitle.setText("赚积分");
        getData();
    }

    private void getData() {
        mPresenter.getSignData(Account.getInstance().getUserid());
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    @OnClick({R.id.iv_back, R.id.rlyt_score_lottery, R.id.tv_sign_in})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rlyt_score_lottery:
                startActivity(ScoreLotteryActivity.getStartIntent(SignInActivity.this));
                break;
            case R.id.tv_sign_in:
                if (mSignInDataBean == null || TextUtils.equals("0", mSignInDataBean.getIsSign())) {
                    dialog = new SignInDialog(SignInActivity.this);
                    dialog.setData(new SignInDialog.Callback() {
                        @Override
                        public void onSubmit() {
                            mPresenter.signIn(Account.getInstance().getUserid());
                        }

                        @Override
                        public void onCancle() {

                        }
                    });
                    dialog.show();
                } else {
                    mPresenter.signIn(Account.getInstance().getUserid());
                }

                break;
            default:
                break;
        }
    }

    @Override
    public void showSignInMsg(String msg) {
        toast(msg);
        mPresenter.getSignData(Account.getInstance().getUserid());
    }

    @Override
    public void showSignData(final SignInDataBean signInDataBean) {
        mSignInDataBean = signInDataBean;
        if (TextUtils.equals("1", signInDataBean.getIsSign())) {
            mTvSignIn.setBackgroundResource(R.drawable.black_circle);
            mTvSignIn.setText("签到成功");
        } else {
            mTvSignIn.setBackgroundResource(R.drawable.red_circle);
            mTvSignIn.setText("签到");

        }
        if (signInDataBean.getIntegral() != null) {
            mTvScore.setText(signInDataBean.getIntegral());
        }

        mTvDays.setText(signInDataBean.getKeepSignDays() + "");
        showContinueSignInScore(signInDataBean.getDayTime());


    }

    private void showContinueSignInScore(int days) {
        switch (days) {
            case 1:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.money_icon);
                mIvThird.setImageResource(R.mipmap.gift_icon);
                mIvFour.setImageResource(R.mipmap.money_icon);
                mIvFive.setImageResource(R.mipmap.money_icon);
                mIvSix.setImageResource(R.mipmap.money_icon);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
            case 2:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.sign_in_success);
                mIvThird.setImageResource(R.mipmap.gift_icon);
                mIvFour.setImageResource(R.mipmap.money_icon);
                mIvFive.setImageResource(R.mipmap.money_icon);
                mIvSix.setImageResource(R.mipmap.money_icon);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
            case 3:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.sign_in_success);
                mIvThird.setImageResource(R.mipmap.sign_in_success);
                mIvFour.setImageResource(R.mipmap.money_icon);
                mIvFive.setImageResource(R.mipmap.money_icon);
                mIvSix.setImageResource(R.mipmap.money_icon);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
            case 4:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.sign_in_success);
                mIvThird.setImageResource(R.mipmap.sign_in_success);
                mIvFour.setImageResource(R.mipmap.sign_in_success);
                mIvFive.setImageResource(R.mipmap.money_icon);
                mIvSix.setImageResource(R.mipmap.money_icon);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
            case 5:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.sign_in_success);
                mIvThird.setImageResource(R.mipmap.sign_in_success);
                mIvFour.setImageResource(R.mipmap.sign_in_success);
                mIvFive.setBackgroundResource(R.mipmap.sign_in_success);
                mIvSix.setImageResource(R.mipmap.money_icon);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
            case 6:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.sign_in_success);
                mIvThird.setImageResource(R.mipmap.sign_in_success);
                mIvFour.setImageResource(R.mipmap.sign_in_success);
                mIvFive.setImageResource(R.mipmap.sign_in_success);
                mIvSix.setImageResource(R.mipmap.sign_in_success);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
            case 7:
                mIvFirst.setImageResource(R.mipmap.sign_in_success);
                mIvSecond.setImageResource(R.mipmap.sign_in_success);
                mIvThird.setImageResource(R.mipmap.sign_in_success);
                mIvFour.setImageResource(R.mipmap.sign_in_success);
                mIvFive.setImageResource(R.mipmap.sign_in_success);
                mIvSix.setImageResource(R.mipmap.sign_in_success);
                mIvSenven.setImageResource(R.mipmap.sign_in_success);
                break;
            default:
                mIvFirst.setImageResource(R.mipmap.money_icon);
                mIvSecond.setImageResource(R.mipmap.money_icon);
                mIvThird.setImageResource(R.mipmap.gift_icon);
                mIvFour.setImageResource(R.mipmap.money_icon);
                mIvFive.setImageResource(R.mipmap.money_icon);
                mIvSix.setImageResource(R.mipmap.money_icon);
                mIvSenven.setImageResource(R.mipmap.gift_icon);
                break;
        }
    }
}
